package com.confinderence;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.confinderence.WebServiceTask.IResponseHandler;
import com.confinderence.model.Lecture;
import com.confinderence.utils.gson.ArrayAdapterFactory;
import com.confinderence.utils.gson.CustomDateTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class MessageActivity extends Activity {
	private TableLayout mTable;
	private WebServiceTask mTask;
	private static final String URL = Utils.REST_URL + "/lmessages/messagesMobile";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		init();
	}

	private void init() {
		mTable = (TableLayout) findViewById(R.id.messagesTable);
		mTask = new WebServiceTask(WebServiceTask.POST_TASK, this, getString(R.string.wait_msg),
				new IResponseHandler() {

					@Override
					public void handleResponse(String response) {
						System.out.println(response);
						Gson gson = new GsonBuilder()
								.registerTypeAdapter(Date.class, new CustomDateTypeAdapter())
								.registerTypeAdapterFactory(new ArrayAdapterFactory()).create();
						Type collectionType = new TypeToken<Collection<Lecture>>() {
						}.getType();
						JSONObject json;
						try {
							json = new JSONObject(response);
							JSONArray array = json.getJSONArray("lecture");
							List<Lecture> lectures = gson.fromJson(array.toString(), collectionType);
							Set<String> messages = new TreeSet<String>();
							for (int i = 0; i < lectures.size(); i++) {
								Lecture lecture = lectures.get(i);
								messages.add("Upcoming Lecture:  " + lecture.getTitle() + " on "
										+ lecture.getDate().toString());
							}

							setTable(messages);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
		// List<String> messages = new ArrayList<>();
		// messages.add("blasila");
		// messages.add("ultralongblasila dmanfa askfm asklgfwqklgqkl wngklqwn gklqwnklgnqwklgnqwlngqwl ngqkl nl");
		// messages.add("krivuntsi");
		// setTable(messages);
		mTask.addNameValuePair("id", "" + Utils.getLecturer().getId());
		mTask.execute(URL);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.logged_in_menu, menu);
		Utils.initMenu(menu, this);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void setTable(Set<String> messages) {
		if (messages != null) {
			int i = 0;
			for (String message : messages) {
				TableRow tr = new TableRow(this);
				addCell(tr, message, 1);
				tr.setBackgroundColor(i % 2 == 0 ? 0xFF8C89D6 : 0xFFCECED6);
				mTable.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT));
				i++;
			}
			mTable.setBackground(getResources().getDrawable(R.drawable.layout_border));
			mTable.requestLayout();
		}
	}

	public void addCell(TableRow tr, String text, float weight) {
		TextView tv = new TextView(this);
		tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, weight));
		tv.setText(text);
		tv.setPadding(10, 5, 10, 5);
		tv.setTextSize(20);
		tr.addView(tv);
		tv.setGravity(Gravity.CENTER_VERTICAL);
	}
}
